package com.kingre.et.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.kingre.et.dao.IIPDAO;
import com.kingre.et.dao.ISchoolDAO;
import com.kingre.et.model.IP;
import com.kingre.et.model.School;
import com.kingre.et.service.IIPService;

@Transactional
public class IPServiceImpl implements IIPService {
    @Autowired
    private ISchoolDAO schoolDAO;
    @Autowired
    private IIPDAO ipDAO;

    public List<School> findSchool() {
    	List<School> list = schoolDAO.findAll();
        if(list == null)
            return null;
        for(int i = 0; i < list.size(); i++){
        	if(list.get(i).getSchoolType() != 10){
        		list.remove(i--);
        	}
        }
        return list;
    }

    public List<IP> find(Integer schoolId, String ip) {
        if(schoolId == null || schoolId == -1)
            return ipDAO.findByName(ip);
        else{
            School school = schoolDAO.get(schoolId);
            return ipDAO.findBySchoolAndName(school, ip);
        }
    }

    public void delete(Integer id) {
        IP ip = ipDAO.get(id);
        ipDAO.delete(ip);
    }

    public void add(IP ip, Integer addSchoolId) {
        if(ip == null)
            return;
        School s = schoolDAO.get(addSchoolId);
        ip.setSchool(s);
        ipDAO.save(ip);
    }

}
